package com.andy.module.base

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.andy.module.loadsir.EmptyCallback
import com.andy.module.loadsir.ErrorCallback
import com.andy.module.loadsir.LoadingCallback
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import me.hgj.jetpackmvvm.state.ResultState

/**
 * @creator:LuoFan
 * @date:2022/6/6
 * @description:
 */
abstract class BaseVbPageListActivity<VM : BasePageListViewModel<T>, VB : ViewBinding, T> : BaseVbActivity<VM, VB>() {

    private lateinit var loadSir: LoadService<*>

    override fun initView(savedInstanceState: Bundle?) {
        val adapter = getAdapter()
        getRecyclerView().adapter = adapter
        val layoutManager = LinearLayoutManager(this)
        getRecyclerView().layoutManager = layoutManager
        getRefreshLayout().setOnRefreshListener {
            mViewModel.requestList(true)
        }
        getRefreshLayout().setOnLoadMoreListener {
            mViewModel.requestList(false)
        }
        mViewModel.listLoadingChange.isRefresh.observeInActivity(this) {
            if (it) {
                getRefreshLayout().finishRefresh()
            } else {
                getRefreshLayout().finishRefreshWithNoMoreData()
            }
        }
        mViewModel.listLoadingChange.isLoadMore.observeInActivity(this) {
            if (it) {
                getRefreshLayout().finishLoadMore()
            } else {
                getRefreshLayout().finishLoadMoreWithNoMoreData()
            }
        }
        loadSir = LoadSir.getDefault().register(getRefreshLayout()) {
            loadSir.showCallback(LoadingCallback::class.java)
            mViewModel.requestList()
        }
        mViewModel.resultList.observe(this) { result ->
            when (result) {
                is ResultState.Success -> {
                    if (result.data.pn == 1) {
                        adapter.setList(result.data.datas)
                    } else {
                        adapter.addData(result.data.datas)
                    }
                    if (adapter.data.size == 0) {
                        loadSir.showCallback(EmptyCallback::class.java)
                    } else {
                        loadSir.showSuccess()
                    }
                }
                else -> {
                    loadSir.showCallback(ErrorCallback::class.java)
                    //下拉刷新出错的话,清空原数据
                    if (mViewModel.getPageNum() == 1) {
                        adapter.setList(mutableListOf())
                    }
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        if (autoLoad()) {
            mViewModel.requestList()
        }
    }

    open fun autoLoad() = true

    abstract fun getAdapter(): BaseQuickAdapter<T, BaseViewHolder>

    abstract fun getRecyclerView(): RecyclerView

    abstract fun getRefreshLayout(): SmartRefreshLayout

}